package cn.iocoder.yudao.module.scrm.dal.mysql.order;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.scrm.controller.admin.clue.vo.CluePageReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.business.BusinessDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.clue.ClueDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.order.OrderDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.scrm.controller.admin.order.vo.*;
import org.springframework.util.StringUtils;

/**
 * 订单 Mapper
 *
 * @author admin
 */
@Mapper
public interface OrderMapper extends BaseMapperX<OrderDO> {

    /*default PageResult<OrderDO> selectPage(OrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrderDO>()
                .eqIfPresent(OrderDO::getTitle, reqVO.getTitle())
                .eqIfPresent(OrderDO::getCustomerId, reqVO.getCustomerId())
                .eqIfPresent(OrderDO::getProductId, reqVO.getProductId())
                .eqIfPresent(OrderDO::getOrderTotalMoney, reqVO.getOrderTotalMoney())
                .betweenIfPresent(OrderDO::getSigningDate, reqVO.getSigningDate())
                .eqIfPresent(OrderDO::getOrderType, reqVO.getOrderType())
                .betweenIfPresent(OrderDO::getStartDate, reqVO.getStartDate())
                .betweenIfPresent(OrderDO::getExpireDate, reqVO.getExpireDate())
                .eqIfPresent(OrderDO::getClientSignatory, reqVO.getClientSignatory())
                .eqIfPresent(OrderDO::getOurSignatory, reqVO.getOurSignatory())
                .eqIfPresent(OrderDO::getPaymentMethod, reqVO.getPaymentMethod())
                .eqIfPresent(OrderDO::getRemark, reqVO.getRemark())
                .eqIfPresent(OrderDO::getSystemNumber, reqVO.getSystemNumber())
                .eqIfPresent(OrderDO::getOwnerUserId, reqVO.getOwnerUserId())
                .eqIfPresent(OrderDO::getOldOwnerUserId, reqVO.getOldOwnerUserId())
                .eqIfPresent(OrderDO::getInstanceId, reqVO.getInstanceId())
                .eqIfPresent(OrderDO::getInstanceIfSubmit, reqVO.getInstanceIfSubmit())
                .eqIfPresent(OrderDO::getInstanceState, reqVO.getInstanceState())
                .eqIfPresent(OrderDO::getFileUrl, reqVO.getFileUrl())
                .eqIfPresent(OrderDO::getVField, reqVO.getVField())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderDO::getId));
    }*/

    //key与其它参数是or的关系
    /*default PageResult<OrderDO> selectPage(OrderPageReqVO reqVO) {
        LocalDateTime startOfDay = (reqVO.getSigningDate() != null && reqVO.getSigningDate().length > 0) ?
                reqVO.getSigningDate()[0].toLocalDate().atStartOfDay() : null;
        LocalDateTime endOfDay = (reqVO.getSigningDate() != null && reqVO.getSigningDate().length > 0) ?
                reqVO.getSigningDate()[0].toLocalDate().atTime(LocalTime.MAX) : null;

        LocalDateTime startOfDayCreateTime = (reqVO.getCreateTime() != null && reqVO.getCreateTime().length > 0) ?
                reqVO.getCreateTime()[0].toLocalDate().atStartOfDay() : null;
        LocalDateTime endOfDayCreateTime = (reqVO.getCreateTime() != null && reqVO.getCreateTime().length > 0) ?
                reqVO.getCreateTime()[0].toLocalDate().atTime(LocalTime.MAX) : null;

        List<String> userIds = reqVO.getUserId();

        return selectPage(reqVO, new LambdaQueryWrapperX<OrderDO>()
                     .like(StringUtils.hasText(reqVO.getKey()),OrderDO::getSystemNumber, reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()),OrderDO::getTitle, reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()),OrderDO::getOrderTotalMoney,reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()),OrderDO::getClientSignatory,reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()),OrderDO::getOurSignatory,reqVO.getKey())
                .or().like(StringUtils.hasText(reqVO.getKey()),OrderDO::getRemark,reqVO.getKey())

                .between(startOfDay != null && endOfDay!=null, OrderDO::getSigningDate, startOfDay, endOfDay)
                .between(startOfDayCreateTime != null && endOfDayCreateTime!=null, OrderDO::getCreateTime, startOfDayCreateTime, endOfDayCreateTime)
                .eq(StringUtils.hasText(reqVO.getPaymentMethod()),OrderDO::getPaymentMethod, reqVO.getPaymentMethod())

                .in(userIds!=null && !userIds.isEmpty(),OrderDO::getOwnerUserId,reqVO.getUserId())

                .orderByDesc(OrderDO::getCreateTime));
    }*/

    //key与其它参数是并且的关系
    default PageResult<OrderDO> selectPage(OrderPageReqVO reqVO) {
        LocalDateTime startOfDay = (reqVO.getSigningDate() != null && reqVO.getSigningDate().length > 0) ?
                reqVO.getSigningDate()[0].toLocalDate().atStartOfDay() : null;
        LocalDateTime endOfDay = (reqVO.getSigningDate() != null && reqVO.getSigningDate().length > 0) ?
                reqVO.getSigningDate()[0].toLocalDate().atTime(LocalTime.MAX) : null;

        LocalDateTime startOfDayCreateTime = (reqVO.getCreateTime() != null && reqVO.getCreateTime().length > 0) ?
                reqVO.getCreateTime()[0].toLocalDate().atStartOfDay() : null;
        LocalDateTime endOfDayCreateTime = (reqVO.getCreateTime() != null && reqVO.getCreateTime().length > 0) ?
                reqVO.getCreateTime()[0].toLocalDate().atTime(LocalTime.MAX) : null;

        List<String> userIds = reqVO.getUserId();
        String key = reqVO.getKey();

        LambdaQueryWrapperX<OrderDO> queryWrapper = new LambdaQueryWrapperX<>();

        // 如果有 key，设置多个 like 条件
        if (StringUtils.hasText(key)) {
            queryWrapper.and(query -> query
                    .like(OrderDO::getSystemNumber, reqVO.getKey())
                    .or().like(OrderDO::getTitle, reqVO.getKey())
                    .or().like(OrderDO::getOrderTotalMoney, reqVO.getKey())
                    .or().like(OrderDO::getClientSignatory, reqVO.getKey())
                    .or().like(OrderDO::getOurSignatory, reqVO.getKey())
                    .or().like(OrderDO::getRemark, reqVO.getKey()));
        }

        // 添加其他查询条件
        queryWrapper
                .between(startOfDay != null && endOfDay != null, OrderDO::getSigningDate, startOfDay, endOfDay)
                .between(startOfDayCreateTime != null && endOfDayCreateTime != null, OrderDO::getCreateTime, startOfDayCreateTime, endOfDayCreateTime)
                .eq(StringUtils.hasText(reqVO.getPaymentMethod()), OrderDO::getPaymentMethod, reqVO.getPaymentMethod())
                .in(userIds != null && !userIds.isEmpty(), OrderDO::getOwnerUserId, userIds)
                .orderByDesc(OrderDO::getCreateTime);

        return selectPage(reqVO, queryWrapper);
    }

}